/*
 * JVM_Bitmap2D.c
 *
 *  Created on: 02.05.2011
 *      Author: Tavin
 */


#include <stdint.h>

#include "JVM_Handle.h"
#include "JVM_Opcode.h"
#include "JVM_StatusCodes.h"
#include "JVM_Bitmap2D.h"
#include "Bitmap2D.h"



/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap3D_fill							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.4.29			Status : working   			*/
/*																						*/
/****************************************************************************************/

uint16_t JVM_Bitmap2D_fill()
{
	JVM_HandleNumber_t bitmapHandle;
	uint16_t color;
	uint16_t size;
	uint16_t  * addres;


	color=JVM_Opcode_popIntValue();
	bitmapHandle=JVM_Opcode_popReference();


	 JVM_Handle_getRealAddres(bitmapHandle,(uint32_t*)&addres);

	 addres+=2;
	 size=*addres++;
	 addres+=2;

	Bitmap2D_fill((uint16_t*) addres, color,size-2);



	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap3D_setPixel						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.4.29			Status : working   			*/
/*																						*/
/****************************************************************************************/

uint16_t JVM_Bitmap2D_setPixel()
{


	Pixel2D_t pixel;

	JVM_HandleNumber_t bitmapHandle;
	uint16_t color;
	uint16_t size;
	uint16_t  * addres;

	color=JVM_Opcode_popIntValue();
	bitmapHandle=JVM_Opcode_popReference();
	pixel.position.y=JVM_Opcode_popIntValue();
	pixel.position.x=JVM_Opcode_popIntValue();


	JVM_Handle_getRealAddres(bitmapHandle,(uint32_t*)&addres);

	addres+=2;
	size=*addres++;
	pixel.displaySize.x=*addres++;
	pixel.displaySize.y=*addres++;


	Bitmap2D_setPixel(pixel,addres,color);
	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap3D_getPixel							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.4.29			Status : working   			*/
/*																						*/
/****************************************************************************************/

uint16_t JVM_Bitmap2D_getPixel()
{

	Pixel2D_t pixel;

	JVM_HandleNumber_t bitmapHandle;
	uint16_t color;
	uint16_t size;
	uint16_t  * addres;


	bitmapHandle=JVM_Opcode_popReference();
	pixel.position.y=JVM_Opcode_popIntValue();
	pixel.position.x=JVM_Opcode_popIntValue();

	JVM_Handle_getRealAddres(bitmapHandle,(uint32_t*)&addres);

	addres+=2;
	size=*addres++;
	pixel.displaySize.x=*addres++;
	pixel.displaySize.y=*addres++;

	color=Bitmap2D_getPixel(pixel,addres);

	JVM_Opcode_pushIntValue(color);

	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap2D_mirror							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.4.29			Status : working   			*/
/*																						*/
/****************************************************************************************/


uint16_t JVM_Bitmap2D_mirror()
{
	JVM_HandleNumber_t bitmapHandle;
	uint8_t axis;
	uint16_t  * addres;

	Dimension2D_t size;

	bitmapHandle=JVM_Opcode_popReference();
	axis=JVM_Opcode_popIntValue();

	JVM_Handle_getRealAddres(bitmapHandle,(uint32_t*)&addres);


	addres+=3;
	size.x=*addres++;
	size.y=*addres++;


	switch (axis)
		{
		case 0:
			Bitmap2D_mirrorX(addres,&size);
			break;

		case 1:
			Bitmap2D_mirrorY(addres,&size);
			break;

		}
	return(STCD_OK);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap2D_drawLine						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.6.12			Status : working   			*/
/*																						*/
/****************************************************************************************/


uint16_t JVM_Bitmap2D_drawLine()
{

	JVM_HandleNumber_t bitmapHandle;
	uint16_t xPos;
	uint16_t yPos;

	uint16_t xSize;
	uint16_t ySize;
	uint16_t color;

	uint16_t * addres;
	Dimension2D_t size;

	color=JVM_Opcode_popIntValue();
	ySize=JVM_Opcode_popIntValue();
	xSize=JVM_Opcode_popIntValue();



	yPos=JVM_Opcode_popIntValue();
	xPos=JVM_Opcode_popIntValue();

	bitmapHandle=JVM_Opcode_popReference();
	JVM_Handle_getRealAddres(bitmapHandle,(uint32_t*)&addres);

	addres+=3;
	size.x=*addres++;
	size.y=*addres++;


	Bitmap2D_drawLine(xPos,yPos,xSize,ySize,addres,&size,color );


	return(STCD_OK);
}



/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap2D_not								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.2			Status : working   			*/
/*																						*/
/****************************************************************************************/

uint16_t JVM_Bitmap2D_not()
{
	JVM_HandleNumber_t destinationHandle;



	uint16_t  * destinationAddres;
	uint16_t size;


	destinationHandle=JVM_Opcode_popReference();

	JVM_Handle_getRealAddres(destinationHandle,(uint32_t*)&destinationAddres);


	destinationAddres+=2;
	size=*destinationAddres++;
	destinationAddres+=2;



	Bitmap2D_not(destinationAddres,size);

	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap2D_min								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.2			Status : working   			*/
/*																						*/
/****************************************************************************************/

uint16_t JVM_Bitmap2D_min()
{
	JVM_HandleNumber_t destinationHandle;
	JVM_HandleNumber_t connectiveHandle;

	Dimension2D_t destinationSize;
	Dimension2D_t connectiveSize;

	uint16_t  * connectiveAddres;
	uint16_t  * destinationAddres;


	connectiveHandle=JVM_Opcode_popReference();
	destinationHandle=JVM_Opcode_popReference();

	JVM_Handle_getRealAddres(connectiveHandle,(uint32_t*)&connectiveAddres);
	JVM_Handle_getRealAddres(destinationHandle,(uint32_t*)&destinationAddres);


	destinationAddres+=3;
	destinationSize.x=*destinationAddres++;
	destinationSize.y=*destinationAddres++;



	connectiveAddres+=3;
	connectiveSize.x=*connectiveAddres++;
	connectiveSize.y=*connectiveAddres++;


	Bitmap2D_min(destinationAddres, connectiveAddres,&destinationSize,&connectiveSize);

	return(STCD_OK);
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap2D_max								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.2			Status : working   			*/
/*																						*/
/****************************************************************************************/

uint16_t JVM_Bitmap2D_max()
{
	JVM_HandleNumber_t destinationHandle;
	JVM_HandleNumber_t connectiveHandle;

	Dimension2D_t destinationSize;
	Dimension2D_t connectiveSize;

	uint16_t  * connectiveAddres;
	uint16_t  * destinationAddres;


	connectiveHandle=JVM_Opcode_popReference();
	destinationHandle=JVM_Opcode_popReference();

	JVM_Handle_getRealAddres(connectiveHandle,(uint32_t*)&connectiveAddres);
	JVM_Handle_getRealAddres(destinationHandle,(uint32_t*)&destinationAddres);


	destinationAddres+=3;
	destinationSize.x=*destinationAddres++;
	destinationSize.y=*destinationAddres++;



	connectiveAddres+=3;
	connectiveSize.x=*connectiveAddres++;
	connectiveSize.y=*connectiveAddres++;


	Bitmap2D_max(destinationAddres, connectiveAddres,&destinationSize,&connectiveSize);

	return(STCD_OK);
}





/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap2D_print5x5Char					Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.28			Status : working   			*/
/*																						*/
/****************************************************************************************/

uint16_t JVM_Bitmap2D_print5x5Char()
{
	JVM_HandleNumber_t bitmapHandle;
	uint16_t  * bitmapAddres;
	uint8_t index;
	uint16_t color;
	boolean returnValue;

	 Dimension2D_t origin;
	 Dimension2D_t bitmapSize;

	 origin.y=JVM_Opcode_popReference();
	 origin.x=JVM_Opcode_popReference();
	 color=JVM_Opcode_popReference();
	 index=JVM_Opcode_popReference();

	 bitmapHandle=JVM_Opcode_popReference();


	JVM_Handle_getRealAddres(bitmapHandle,(uint32_t*)&bitmapAddres);

	bitmapAddres+=3;
	bitmapSize.x=*bitmapAddres++;
	bitmapSize.y=*bitmapAddres++;

	returnValue=Bitmap2D_print5x5Char(bitmapAddres, &origin,&bitmapSize,index,color);

	JVM_Opcode_pushIntValue(returnValue);

	return(STCD_OK);
}



/****************************************************************************************/
/*																						*/
/*	Function : JVM_Bitmap2D_print8x8Char					Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.28			Status : working   			*/
/*																						*/
/****************************************************************************************/

uint16_t JVM_Bitmap2D_print8x8Char()
{
	JVM_HandleNumber_t bitmapHandle;
	uint16_t  * bitmapAddres;
	uint8_t index;
	uint16_t color;
	boolean returnValue;

	 Dimension2D_t origin;
	 Dimension2D_t bitmapSize;

	 origin.y=JVM_Opcode_popReference();
	 origin.x=JVM_Opcode_popReference();
	 color=JVM_Opcode_popReference();
	 index=JVM_Opcode_popReference();

	 bitmapHandle=JVM_Opcode_popReference();


	JVM_Handle_getRealAddres(bitmapHandle,(uint32_t*)&bitmapAddres);
//[SCSII)Z
	bitmapAddres+=3;
	bitmapSize.x=*bitmapAddres++;
	bitmapSize.y=*bitmapAddres++;

	returnValue=Bitmap2D_print8x8Char(bitmapAddres, &origin,&bitmapSize,index,color);

	JVM_Opcode_pushIntValue(returnValue);

	return(STCD_OK);
}

